#include "stdafx.h"
#include "RTWindowApplication.h"
#include "RTFramework.h"
#include "RTMacro.h"

const TCHAR* RTCLASSNAME = _T("RTEngine Framework");

RTWindowApplication::RTWindowApplication()
:	m_hWnd( NULL ),
	m_hInstance( NULL ),
	m_dwWindowStyle( 0 ),
	m_nWidth( 0 ),
	m_nHeight( 0 ),
	m_bWindowed( TRUE ),
	m_pszClassName( NULL ),
	m_pszCaptionName( NULL )
{

}

RTWindowApplication::~RTWindowApplication()
{
	Release();
}

BOOL	RTWindowApplication::Initialize( TCHAR* pszCaptionName, UINT nWidth, UINT nHeight, BOOL bWindowed, DWORD dwWindowStyle )
{
	RTSAFE_DELETE_ARRAY( m_pszCaptionName );
	if( !pszCaptionName )
		pszCaptionName = _T("RealTime Engine");

	size_t len = _tcslen( pszCaptionName ) + 1;
	m_pszCaptionName = new TCHAR[ len ];
	_tcscpy_s( m_pszCaptionName, len, pszCaptionName );

	len = _tcslen( RTCLASSNAME ) + 1;
	m_pszClassName = new TCHAR[ len ];
	_tcscpy_s( m_pszClassName, len, RTCLASSNAME );

	m_nWidth = nWidth;
	m_nHeight = nHeight;
	m_bWindowed = bWindowed;
	if( !m_bWindowed )
		m_dwWindowStyle = WS_POPUP;
	else
		m_dwWindowStyle = dwWindowStyle;

	return TRUE;
}

BOOL	RTWindowApplication::Create( HINSTANCE hInstance )
{
	WNDCLASSEX wc = 
	{
		sizeof(WNDCLASSEX), CS_CLASSDC, 
		RTFramework::WinProc, 0L, 0L,
		hInstance, NULL, NULL, NULL, NULL,
		m_pszClassName, NULL
	};

	RegisterClassEx( &wc );

	m_hWnd = CreateWindow( m_pszClassName, m_pszCaptionName,
		m_dwWindowStyle, 0, 0, m_nWidth, m_nHeight,
		NULL, NULL, hInstance, NULL );

	return TRUE;
}

BOOL	RTWindowApplication::Release()
{
	m_hWnd = NULL;
	m_hInstance = NULL;

	RTSAFE_DELETE_ARRAY( m_pszClassName );
	RTSAFE_DELETE_ARRAY( m_pszCaptionName );

	return TRUE;
}

UINT	RTWindowApplication::GetWidth() const
{
	return m_nWidth;
}

UINT	RTWindowApplication::GetHeight() const
{
	return m_nHeight;
}

BOOL	RTWindowApplication::IsWindowedMode() const
{
	return m_bWindowed;
}

HWND	RTWindowApplication::GetHWND() const
{
	return m_hWnd;
}
